package com.lucy.nepu.module.raward.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.nepu.module.raward.dao.RAwardDAO;
import com.lucy.nepu.module.raward.entity.RAward;
import com.lucy.nepu.module.raward.event.RAwardCreatedEvent;
import com.lucy.nepu.module.raward.event.RAwardDeletedEvent;
import com.lucy.nepu.module.raward.service.RAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class RAwardServiceImpl implements RAwardService {
    @Autowired
    RAwardDAO dao;

    @Resource
    ApplicationContext context;

    @Override
    public RAward getOneUnwrap(String s) {
        Optional<RAward> awardOp = dao.findById(s);
        if (!awardOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return awardOp.get();
    }


    @Override
    public PageResultDTO<RAward> getAll(Pageable pageable, Example<RAward> example) {
        Page<RAward> page;

        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }
        else {
            page = dao.findAll(example,pageable);
        }
        /*
          获取分页主体内容
         */
        List<RAward> list = page.getContent();
        PageResultDTO<RAward> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<RAward> getExample(Example<RAward> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        RAward entity = getOneUnwrap(id);

        dao.deleteById(id);
        // 事件发布
        context.publishEvent(new RAwardDeletedEvent(this,entity));
    }

    @Override
    public Optional<RAward> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public RAward saveOne(RAward o) {
        if (Validator.isNotEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"新建DTO不可携带ID");
        }
        RAward saveRes= dao.save(o);
        // 事件发布
        context.publishEvent(new RAwardCreatedEvent(this,saveRes));
        return saveRes;
    }

    @Override
    public RAward updateOne(RAward o) {
        if (Validator.isEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"更新DTO必须携带ID");
        }
        return dao.save(o);
    }
}
